package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.apache.ibatis.annotations.Param;
import org.linlinjava.litemall.db.dao.LitemallCommissionLogMapper;
import org.linlinjava.litemall.db.dao.LitemallCommissionMapper;
import org.linlinjava.litemall.db.domain.LitemallCommission;
import org.linlinjava.litemall.db.domain.LitemallCommissionApplyExample;
import org.linlinjava.litemall.db.domain.LitemallCommissionLog;
import org.linlinjava.litemall.db.domain.LitemallCommissionLogExample;
import org.linlinjava.litemall.db.vo.PageVO;
import org.linlinjava.litemall.db.vo.UserCommissionVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
 * Created by 75720 on 2019/05/21.
 */
@Component
public class LitemallCommissionLogService {
    @Autowired
    private LitemallCommissionLogMapper commissionLogMapper;
    @Autowired
    private LitemallCommissionMapper commissionMapper;

    public List<LitemallCommissionLog> queryByExample(PageVO<LitemallCommissionLog, LitemallCommissionLog> page) {
        if (page.getSize() == null) {
            page.setSize(10);
        }
        if (page.getPage() == null) {
            page.setPage(0);
        }
        if (page.getCondition() != null) {
        }
        LitemallCommissionLogExample example = new LitemallCommissionLogExample();
        LitemallCommissionLogExample.Criteria criteria = example.createCriteria();
        if (page.getBeginTime() != null) {
            criteria.andCreatedTimeGreaterThanOrEqualTo(page.getBeginTime());
        }
        if (page.getEndTime() != null) {
            criteria.andCreatedTimeLessThanOrEqualTo(page.getEndTime());
        }
        if (page.getCondition() != null) {
            LitemallCommissionLog condition = page.getCondition();
            if (condition.getUserId() != null) {
                criteria.andUserIdEqualTo(condition.getUserId());
            }
            if (condition.getFromUid() != null) {
                criteria.andFromUidEqualTo(condition.getFromUid());
            }
            if (condition.getType() != null) {
                criteria.andTypeEqualTo(condition.getType());
            }
        }
        PageHelper.startPage(page.getPage(), page.getSize());
        return commissionLogMapper.queryByExample(example);
    }

    /**
     * 查询用户佣金情况
     * @return
     */
    public List<UserCommissionVO> queryUserCommission(PageVO<LitemallCommissionLog, LitemallCommissionLog> page) {
        if (page.getSize() == null) {
            page.setSize(10);
        }
        if (page.getPage() == null) {
            page.setPage(0);
        }
        List<UserCommissionVO> userCommissionVOList = new ArrayList<UserCommissionVO>();
        LitemallCommissionLogExample example = new LitemallCommissionLogExample();
        LitemallCommissionLogExample.Criteria criteria = example.createCriteria();
        if (page.getBeginTime() != null) {
            criteria.andCreatedTimeGreaterThanOrEqualTo(page.getBeginTime());
        }
        if (page.getEndTime() != null) {
            criteria.andCreatedTimeLessThanOrEqualTo(page.getEndTime());
        }
        if (page.getCondition() != null) {
            LitemallCommissionLog condition = page.getCondition();
            if (condition.getUserId() != null) {
                criteria.andUserIdEqualTo(condition.getUserId());
            }
            if (condition.getFromUid() != null) {
                criteria.andFromUidEqualTo(condition.getFromUid());
            }
        }
        PageHelper.startPage(page.getPage(), page.getSize());
        List<LitemallCommissionLog> logList = commissionLogMapper.queryByExample(example);
        HashMap<Integer,UserCommissionVO> resultMap = new HashMap<Integer, UserCommissionVO>();
        UserCommissionVO vo = new UserCommissionVO();
        if (logList != null && !logList.isEmpty()) {
            for(LitemallCommissionLog log : logList) {
                vo = resultMap.get(vo.getUserId());
                if(vo == null) {
                    vo = new UserCommissionVO();
                }
                if("0".equals(log.getType())) {
                    vo.setIncome(log.getCommission());
                } else if("1".equals(log.getType())) {
                    vo.setApply(log.getCommission());
                }
                vo.setUserId(log.getUserId());
                resultMap.put(log.getUserId(), vo);
            }
        }
        if(resultMap != null) {
            for(Integer userId : resultMap.keySet()) {
                userCommissionVOList.add(resultMap.get(userId));
            }
        }
        return userCommissionVOList;
    }

    int update(LitemallCommissionLog log) {
        return commissionLogMapper.update(log);
    }

    int save(LitemallCommissionLog log) {
        return commissionLogMapper.save(log);
    }
}
